# method_interfaces.py

import functools

from Tkinter import *
import ttk

from ot_simulator.tk_gui.toplevel import toplevel

class base(object):
    def __init__(self, label, fn):
        self.label = label
        self.fn = fn

    def render(self, obj, frame, row, col):
        ttk.Button(frame, text=self.label,
                   command=functools.partial(self.doit, obj)) \
          .grid(row=row, column=col)
        return nada, False, False

class method_without_parameters(base):
    def doit(self, obj):
        self.fn(obj)

class method_with_parameters(base):
    def __init__(self, label, fn, *input_fields):
        base.__init__(self, label, fn)
        self.input_fields = input_fields

    def doit(self, obj):
        toplevel.method_caller(self.input_fields, self.label,
                               functools.partial(self.call_fn, obj))

    def call_fn(self, obj, frame, args):
        #print "method_with_parameters.call_fn", obj, args
        self.fn(obj, *args)
        frame.destroy()

def methodcaller(method_name):
    def surrogate(obj, *args):
        getattr(obj, method_name)(*args)
    return surrogate

def nada(): pass
